package br.com.upper.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.upper.util.HibernateUtil;

public abstract class AbstractHibernateDAO<ENTITY, PK extends Serializable> {
	
	protected final Class entityClass;
	
	public AbstractHibernateDAO(Class entityClass) {
		this.entityClass = entityClass;
	}

    public void save(ENTITY entity) {
        this.getSession().saveOrUpdate(entity);
    }
 
    public void delete(ENTITY entity) {
        this.getSession().delete(entity);
    }
 
    public ENTITY findByID(PK id) {
        ENTITY object = null;
        object = (ENTITY) this.getSession().get(this.entityClass, id);
        return object;
    }
 
    public List<ENTITY> findAll() {
        List<ENTITY> list = null;
        Query query = this.getSession().createQuery("from " + this.entityClass.getName());
        list = query.list();
        return list;
    }
	
	protected Session getSession() {
		return HibernateUtil.getSession();
	}
	
}
